<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['pointRecord:pointRecord:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pointRecord:pointRecord:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pointRecord:pointRecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['pointRecord:pointRecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="自增ID" align="center" prop="id" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="商品ID" align="center" prop="productId" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品图片" align="center" prop="productImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.productImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="消耗积分" align="center" prop="pointsCost" />
      <el-table-column label="兑换数量" align="center" prop="exchangeQuantity" />
      <!-- <el-table-column label="兑换时间" align="center" prop="exchangeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.exchangeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="收货地址" align="center" prop="deliveryAddress" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="兑换状态" align="center" prop="exchangeStatus">
        <template #default="scope">
          <dict-tag :options="exchange_status" :value="scope.row.exchangeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="物流追踪号" align="center" prop="trackingNumber" />
      <el-table-column label="物流公司" align="center" prop="logisticsCompany" />
      <!-- <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="completionTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.completionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="取消原因" align="center" prop="cancelReason" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['pointRecord:pointRecord:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['pointRecord:pointRecord:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分兑换记录对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="pointRecordRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="用户ID" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入用户ID" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品ID" prop="productId">
              <el-input v-model="form.productId" placeholder="请输入商品ID" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入商品名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="消耗积分" prop="pointsCost">
              <el-input v-model="form.pointsCost" placeholder="请输入消耗积分" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="兑换数量" prop="exchangeQuantity">
              <el-input v-model="form.exchangeQuantity" placeholder="请输入兑换数量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="兑换时间" prop="exchangeTime">
              <el-date-picker clearable
                v-model="form.exchangeTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择兑换时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="收货地址" prop="deliveryAddress">
              <el-input v-model="form.deliveryAddress" placeholder="请输入收货地址" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="兑换状态" prop="exchangeStatus">
              <el-radio-group v-model="form.exchangeStatus">
                <el-radio
                  v-for="dict in exchange_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="物流追踪号" prop="trackingNumber">
              <el-input v-model="form.trackingNumber" placeholder="请输入物流追踪号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物流公司" prop="logisticsCompany">
              <el-input v-model="form.logisticsCompany" placeholder="请输入物流公司" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发货时间" prop="deliveryTime">
              <el-date-picker clearable
                v-model="form.deliveryTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择发货时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="完成时间" prop="completionTime">
              <el-date-picker clearable
                v-model="form.completionTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="取消原因" prop="cancelReason">
              <el-input v-model="form.cancelReason" placeholder="请输入取消原因" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="商品图片" prop="productImage">
              <image-upload v-model="form.productImage"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注信息" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 新增详情弹窗 -->
    <el-dialog title="积分兑换记录详情" v-model="detailOpen" width="700px" append-to-body>
      <el-descriptions :column="3" border>
        <el-descriptions-item label="自增ID">{{ detailData.id }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ detailData.userId }}</el-descriptions-item>
        <el-descriptions-item label="商品ID">{{ detailData.productId }}</el-descriptions-item>
        <el-descriptions-item label="商品名称">{{ detailData.productName }}</el-descriptions-item>
        <el-descriptions-item label="消耗积分">{{ detailData.pointsCost }}</el-descriptions-item>
        <el-descriptions-item label="兑换数量">{{ detailData.exchangeQuantity }}</el-descriptions-item>
        <el-descriptions-item label="兑换时间">{{ parseTime(detailData.exchangeTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="收货地址">{{ detailData.deliveryAddress }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ detailData.contactPhone }}</el-descriptions-item>
        <el-descriptions-item label="兑换状态">
          <dict-tag :options="exchange_status" :value="detailData.exchangeStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="物流追踪号">{{ detailData.trackingNumber }}</el-descriptions-item>
        <el-descriptions-item label="物流公司">{{ detailData.logisticsCompany }}</el-descriptions-item>
        <el-descriptions-item label="发货时间">{{ parseTime(detailData.deliveryTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="完成时间">{{ parseTime(detailData.completionTime, '{y}-{m}-{d}') }}</el-descriptions-item>
        <el-descriptions-item label="取消原因">{{ detailData.cancelReason }}</el-descriptions-item>
        <el-descriptions-item label="商品图片" :span="3">
          <image-preview :src="detailData.productImage" :width="80" :height="80"/>
        </el-descriptions-item>
        <el-descriptions-item label="备注信息" :span="3">{{ detailData.remark }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PointRecord">
import { listPointRecord, getPointRecord, delPointRecord, addPointRecord, updatePointRecord } from "@/api/pointRecord/pointRecord";
import { parseTime } from "@/utils/ruoyi";

const { proxy } = getCurrentInstance();
const { exchange_status } = proxy.useDict('exchange_status');

const pointRecordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const detailOpen = ref(false);
const detailData = ref({});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productName: null,
    contactPhone: null,
  },
  rules: {
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "商品ID不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "商品名称不能为空", trigger: "blur" }
    ],
    pointsCost: [
      { required: true, message: "消耗积分不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询积分兑换记录列表 */
function getList() {
  loading.value = true;
  listPointRecord(queryParams.value).then(response => {
    pointRecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    exchangeCode: null,
    userId: null,
    productId: null,
    productName: null,
    productImage: null,
    pointsCost: null,
    exchangeQuantity: null,
    exchangeTime: null,
    deliveryAddress: null,
    contactPhone: null,
    exchangeStatus: null,
    trackingNumber: null,
    logisticsCompany: null,
    deliveryTime: null,
    completionTime: null,
    cancelReason: null,
    remark: null,
    createdBy: null,
    createdTime: null,
    updatedBy: null,
    updatedTime: null,
    deletedFlag: null
  };
  proxy.resetForm("pointRecordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加积分兑换记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPointRecord(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改积分兑换记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pointRecordRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePointRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPointRecord(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除积分兑换记录编号为"' + _ids + '"的数据项？').then(function() {
    return delPointRecord(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('pointRecord/pointRecord/export', {
    ...queryParams.value
  }, `pointRecord_${new Date().getTime()}.xlsx`)
}

function handleDetail(row) {
  getPointRecord(row.id).then(res => {
    detailData.value = res.data;
    detailOpen.value = true;
  });
}

getList();
</script>
